package com.nba.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nba.service.GameService;
import com.nba.service.HotService;
import com.nba.service.PdateService;
import com.nba.service.PlayerService;
import com.nba.service.TdateService;
import com.nba.service.TeamService;
import com.nba.service.impl.GameServiceImpl;
import com.nba.service.impl.HotServiceImpl;
import com.nba.service.impl.PdateServiceImpl;
import com.nba.service.impl.PlayerServiceImpl;
import com.nba.service.impl.TdateServiceImpl;
import com.nba.service.impl.TeamServiceImpl;

/**
 * Servlet implementation class SearchNewServlet
 */
@WebServlet("/SearchServlet.do")
public class SearchServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String teamKeyword =request.getParameter("teamKeyword");
		int teamPageNum,teamPageSize;
		if(teamKeyword == null) {
			teamKeyword = "";
		}
		if(request.getParameter("teamPageNum") == null) {
			teamPageNum = 1;
		}else {
			teamPageNum = Integer.parseInt(request.getParameter("teamPageNum"));
		}
		
		if(request.getParameter("teamPageSize") == null) {
			teamPageSize = 10;
		}else {
			teamPageSize = Integer.parseInt(request.getParameter("teamPageSize"));
		}
		TeamService teamservice = new TeamServiceImpl();
		request.setAttribute("team", teamservice.getTeam(teamKeyword, teamPageNum, teamPageSize));
		
		String playerKeyword =request.getParameter("playerKeyword");
		int playerPageNum,playerPageSize;
		if(playerKeyword == null) {
			playerKeyword = "";
		}
		if(request.getParameter("playerPageNum") == null) {
			playerPageNum = 1;
		}else {
			playerPageNum = Integer.parseInt(request.getParameter("playerPageNum"));
		}
		
		if(request.getParameter("playerPageSize") == null) {
			playerPageSize = 10;
		}else {
			playerPageSize = Integer.parseInt(request.getParameter("playerPageSize"));
		}
		PlayerService ps = new PlayerServiceImpl(); 
		request.setAttribute("player", ps.getPlayer(playerKeyword, playerPageNum, playerPageSize));
		
		String gameKeyword = request.getParameter("gameKeyWord");
		int gamePageNum,gamePageSize;
		if(gameKeyword == null) {
			gameKeyword = "";
		}
		if(request.getParameter("gamePageNum") == null) {
			gamePageNum = 1;
		}else {
			gamePageNum = Integer.parseInt(request.getParameter("gamePageNum"));
		}
		
		if(request.getParameter("gamePageSize") == null) {
			gamePageSize = 10;
		}else {
			gamePageSize = Integer.parseInt(request.getParameter("gamePageSize"));
		}
		GameService gs = new GameServiceImpl();
		request.setAttribute("game", gs.getScheduleAll(gameKeyword, gamePageNum, gamePageSize));
		
		String tdateKeyword = request.getParameter("tdateKeyword");
		int tdatePageNum,tdatePageSize;
		if(tdateKeyword == null) {
			tdateKeyword = "";
		}
		if(request.getParameter("tdatePageNum") == null) {
			tdatePageNum = 1;
		}else {
			tdatePageNum = Integer.parseInt(request.getParameter("tdatePageNum"));
		}
		
		if(request.getParameter("tdatePageSize") == null) {
			tdatePageSize = 10;
		}else {
			tdatePageSize = Integer.parseInt(request.getParameter("tdatePageSize"));
		}
		TdateService tds = new TdateServiceImpl();
		request.setAttribute("tdate", tds.getTdateAll(tdateKeyword, tdatePageNum, tdatePageSize));
		
		
		String pdateKeyword = request.getParameter("pdateKeyword");
		int pdatePageNum,pdatePageSize;
		if(pdateKeyword == null) {
			pdateKeyword = "";
		}
		if(request.getParameter("pdatePageNum") == null) {
			pdatePageNum = 1;
		}else {
			pdatePageNum = Integer.parseInt(request.getParameter("pdatePageNum"));
		}
		
		if(request.getParameter("pdatePageSize") == null) {
			pdatePageSize = 10;
		}else {
			pdatePageSize = Integer.parseInt(request.getParameter("pdatePageSize"));
		}
		PdateService pds = new PdateServiceImpl();
		request.setAttribute("pdate", pds.getPdateAll(pdateKeyword, pdatePageNum, pdatePageSize));
		
		String hotKeyword = request.getParameter("hotKeyword");
		int hotPageNum,hotPageSize;
		if(hotKeyword == null) {
			hotKeyword = "";
		}
		if(request.getParameter("hotPageNum") == null) {
			hotPageNum = 1;
		}else {
			hotPageNum = Integer.parseInt(request.getParameter("hotPageNum"));
		}
		
		if(request.getParameter("hotPageSize") == null) {
			hotPageSize = 10;
		}else {
			hotPageSize = Integer.parseInt(request.getParameter("hotPageSize"));
		}
		HotService hs = new HotServiceImpl();
		request.setAttribute("hot", hs.getHotAll(hotKeyword, hotPageNum, hotPageSize));
		
		
		request.getRequestDispatcher("bghome.jsp").forward(request, response);
		
	}

}
